/**webSocket通讯管理类 */
class webNetworkMananger {
	/**是否连接 */
	public isConnecteed: boolean;
	public constructor() {

	}
	/**心跳 */
	private hertBeatTimer: egret.Timer;
	/**单例对象 */
	private static instance: webNetworkMananger;
	public static getInstance(): webNetworkMananger {
		if (this.instance == null) {
			this.instance = new webNetworkMananger();
		}
		return this.instance;
	}
	/**心跳 */
	private heartTimer() {
		// this.hertBeatTimer = new egret.Timer(1000, -1);
		// this.hertBeatTimer.addEventListener(egret.TimerEvent.TIMER, e => {
		// 	if (this.isConnecteed == true)
		// 		webNetworkMananger.getInstance().SendMeesage('hert', {});
		// }, this);
		// this.hertBeatTimer.start();
	}

	//游戏场景
	public gameSceneRec: TableUI;
	/**服务器移动 */
	private _root_url: string = "127.0.0.1";
	/**是否联网模式 */
	public static isServerMove: boolean = false;
	private webSocket: egret.WebSocket;
	/**建立连接 */
	public connect(): void {
		if (this._root_url === null) {
			console.error("未设置连接HOST IP");
			return;
		}
		this.webSocket = new egret.WebSocket();
		//设置数据格式为二进制，默认为字符串
		//	this.webSocket.type = egret.WebSocket.TYPE_BINARY;
		//添加收到数据侦听，收到数据会调用此方法
		this.webSocket.addEventListener(egret.ProgressEvent.SOCKET_DATA, this.onReceiveMessage, this);
		//添加链接打开侦听，连接成功会调用此方法
		this.webSocket.addEventListener(egret.Event.CONNECT, this.onSocketOpen, this);
		//添加链接关闭侦听，手动关闭或者服务器关闭连接会调用此方法
		this.webSocket.addEventListener(egret.Event.CLOSE, this.onSocketClose, this);
		//添加异常侦听，出现异常会调用此方法
		this.webSocket.addEventListener(egret.IOErrorEvent.IO_ERROR, this.onSocketError, this);
		//this.webSocket.connect('140.143.211.89', 3111);
		//this.webSocket.connectByUrl('wss://meiruiya12.cn');
		this.webSocket.connectByUrl('ws://172.16.19.104:8083/ws');
	}
	/**连接成功 */
	public onSocketOpen(): void {
		this.isConnecteed = true;
		console.log("连接成功");
		var tt = {
			msgId: 1001,
			userId: 110,
			name: "王石",
			sex: 1,
			headUrl: "周润发"
		};
		this.SendMeesage(JSON.stringify(tt));
	}
	/**连接失败 */
	public onSocketClose(): void {
		this.isConnecteed = false;

		console.log("连接失败");
	}
	/**连接报错 */
	public onSocketError(): void {
		console.log("连接报错");

	}
	/**发送消息（二进制） */
	public SendBitMeesage(mes: egret.ByteArray): void {
		this.webSocket.writeBytes(mes);
		this.webSocket.flush();
	}
	/**发送消息 */
	public SendMeesage(msg: string): void {
		this.webSocket.writeUTF(msg);
	}

	/**接受消息 */
	public onReceiveMessage(e: egret.Event): void {
		// var byte: egret.ByteArray = new egret.ByteArray();
		// this.webSocket.readBytes(byte);
		/**收到的消息 */
		var msg = this.webSocket.readUTF();
		/**解析后的消息 */
		var obj = JSON.parse(msg);


		//登陆消息
		if (obj.msgId == 1101) {
			this.gameSceneRec.waitingMatch();
		}
		//匹配成功，开始游戏
		if (obj.msgId == 1102) {
			var tt = {
				msgId: 1001,
				userIdOne: 110,
				nameOne: "王石",
				sexOne: 1,
				headUrlOne: "周润发",
				userIdTwo: 110,
				nameTwo: "王石",
				sexTwo: 1,
				headUrlTwo: "周润发",
				roomId: 120,
				serveOrder: "0-对方 1-自己"

			};
			this.gameSceneRec.matchSuccessful();
		}




		//报错信息
		if (obj.msgId == 100) {
			this.showErrorMessage(obj);
		}
	}
	/**显示错误信息 */
	public showErrorMessage(errorMessage: string) {
		this.gameSceneRec.errorMsgPopup(errorMessage);
	}
}